/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DTO.Administrador;
import Model.conexionBD;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet(name = "Servlet_Login", urlPatterns = {"/Servlet_Login"})
public class Servlet_Login extends HttpServlet {

  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        Administrador admin;
        String error = null;
        HttpSession s = null;
        conexionBD c;

        try {
            admin = new Administrador();
            admin.setCorreo(request.getParameter("correo"));
            admin.setContrasenia(request.getParameter("pass"));
            s = request.getSession(true);
            s.setAttribute("Administrador", admin);
            c = new conexionBD("root", "root", "SAPCOMED_DATABASE");
            if (c.validar_usuario(admin)) {
                request.getRequestDispatcher("MenDDs.jsp").forward(request, response);
                System.out.println(admin.getNombre());
            } //response.sendRedirect("MenuP.jsp");
            
        } catch (SQLException ex) {
           Logger.getLogger(Servlet_Login.class.getName()).log(Level.SEVERE, null, ex);
            error = "<div style=\"border:1px solid #D8000C;"
                    +"height:20px;"
                    +"width:285px;"
                    +"padding:4px 0px 0px 20px;"
                    +"background-color:#FFBABA;"
                    +"font-weight: bold;"
                    +"color: #D8000C;"
                    +"font-size: 1.1em;"
                    +"margin:30px 0px 0px 0px;\">Usuario no existe o contraseña incorrecta</div>";
            /*error = "Usuario no existe o contraseña incorrecta";*/
           request.setAttribute("error", error);
           request.getRequestDispatcher("index.jsp").forward(request, response);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
